import java.util.Locale;

class Student{
    public String name;
    public int year;

    public Student(String name, int year) {
        this.name = name;
        this.year = year;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", year=" + year +
                '}';
    }
}
public class Test {
    public static void main(String[] args) {
        // 创建一个StringBuilder对象并初始化
        StringBuilder sb = new StringBuilder("Hello");

        // 使用append()方法添加字符串
        sb.append(", World!");
        System.out.println(sb.toString()); // 输出: Hello, World!

        // 使用insert()方法在指定位置插入字符串
        // 在索引5处插入"(Greetings)"，注意索引从0开始
        sb.insert(5, "(Greetings)");
        System.out.println(sb.toString()); // 输出: Hello(Greetings), World!

        // 使用delete()方法删除指定范围内的字符
        // 删除从索引7（包含）到索引18（不包含）的字符
        sb.delete(7, 18);
        System.out.println(sb.toString()); // 输出: Hello(Greetings)

        // 再次使用append()方法添加更多内容
        sb.append(" and welcome to the Java world!");
        System.out.println(sb.toString()); // 输出: Hello(Greetings) and welcome to the Java world!

        // 使用deleteCharAt()方法删除指定位置的字符（作为额外示例）
        // 删除最后一个字符'!'
        sb.deleteCharAt(sb.length() - 1);
        System.out.println(sb.toString()); // 输出: Hello(Greetings) and welcome to the Java world

        // 注意：deleteCharAt()方法只删除一个字符，与delete(int start, int end)方法不同
    }
    public static void main21(String[] args) {
        //在循环中进行字符串拼接时，String在每次拼接时都是创建新的对象，很费时间
        //StringBuffer 和 StringBuilder是根据原来对象进行拼接r
            long start = System.currentTimeMillis();
            String s = "";
            for (int i = 0; i < 100000; i++) {
                s += i;
            }
            long end = System.currentTimeMillis();
            System.out.println(end - start);//2393

            start = System.currentTimeMillis();
            StringBuffer s1 = new StringBuffer("");
            for (int i = 0; i < 100000; i++) {
                s1.append(i);
            }
            end = System.currentTimeMillis();
            System.out.println(end - start);//2


            start = System.currentTimeMillis();
            StringBuilder s2 = new StringBuilder();
            for (int i = 0; i < 100000; i++) {
                s2.append(i);
            }
            end = System.currentTimeMillis();
            System.out.println(end - start);//2
        }

    public static void main20(String[] args) {
        String s="hello world.";
        s+="This is a test!";
        System.out.println(s);//hello world.This is a test!
    }
    public static void main19(String[] args) {
        String s1="herllo world.";
        String s2="This is a test!";
        System.out.println(s1.concat(s2));//herllo world.This is a test!
    }

    public static void main18(String[] args) {
        String s="hello world,This is a test !  ";
        System.out.println(s.endsWith("hello"));//false
        System.out.println(s.endsWith("test"));//false
        System.out.println(s.endsWith("!"));//false
        System.out.println(s.endsWith(" "));//true
    }
    public static void main17(String[] args) {
        String s="hello world,This is a teat ! ";
        System.out.println(s.startsWith("hello"));//true
        System.out.println(s.startsWith("This"));//false
    }
    public static void main16(String[] args) {
        String s="hello world,This is a teat ! ";
        System.out.println(s.contains("This"));//true
    }
    public static void main15(String[] args) {
        String s="  hello world!!  !    ";
        System.out.println(s);
        String ret=s.trim();
        System.out.println(ret);
    }
    public static void main14(String[] args) {
        String s="hello world,This is a teat ! ";
        System.out.println(s.substring(5));// world,This is a teat !
        System.out.println(s.substring(0,5));//hello
    }
    public static void main13(String[] args) {
        String str1="192.168.0.1";
        String[] ret1=str1.split("\\.");
        for (String s1:ret1
             ) {
            System.out.println(s1);
        }
        System.out.println("=============");
        String str2="name=zhangsan&age=10";
        String[] ret2=str2.split("=");//多个分隔符逐步分隔
        for (int i = 0; i < ret2.length; i++) {
            String[] ret3=ret2[i].split("&");
            for (int j = 0; j < ret3.length; j++) {
                System.out.println(ret3[j]);
            }
        }
        System.out.println("=============");
        String[] ret4=str2.split("=|&");//多个分隔符一次分隔，用“|”作为连字符
        for (String s4:ret4
             ) {
            System.out.println(s4);
        }
    }
    public static void main12(String[] args) {
        String s="hello world,This is a teat ! ";
        String[] ret1 =s.split(" ");
        for(String s1:ret1){
            System.out.println(s1);
        }
        System.out.println("=============");
        String[]  ret2=s.split(" ",3);
        for (String s2:ret2
             ) {
            System.out.println(s2);
        }
    }

    public static void main11(String[] args) {
        String s="hello world,This is a teat !";
        System.out.println(s.replace('l','q'));//heqqo worqd,This is a teat !
        System.out.println(s.replace("world","java"));//hello java,This is a teat !
        System.out.println(s.replaceAll("ll","q"));//heqo world,This is a teat !
        System.out.println(s.replaceFirst("l","x"));//hexlo world,This is a teat !
    }
    public static void main10(String[] args) {
        String s1=String.format("%d-%d-%d",2019,9,13);
        System.out.println(s1);//2019-9-13
        String s2=String.format("%d、%d、%d",2019,9,13);
        System.out.println(s2);//2019、9、13
    }
    public static void main9(String[] args) {
        char[] charArray = {'H', 'e', 'l', 'l', 'o'};
        String str = new String(charArray); // 使用构造器将字符数组转化为字符串
        String str2 = String.valueOf(charArray); // 使用valueOf方法将字符数组转化为字符串
        System.out.println(str);
        System.out.println(str2);
    }

    public static void main8(String[] args) {
//        String str = "Hello, World!";
//        char[] charArray = str.toCharArray(); // 将字符串转化为字符数组
//        for (int i = 0; i <charArray.length; i++) {
//            System.out.println(charArray[i]);
//        }
//        System.out.println();

    }

    public static void main7(String[] args) {
        String s1="Hello World";
        //转为大写
        System.out.println(s1.toUpperCase());//HELLO WORLD
        //转为小写
        System.out.println(s1.toLowerCase());//hello world
    }
    public static void main6(String[] args) {
        //数字转字符串
        String s1=String.valueOf(1234);
        String s2=String.valueOf(12.34);
        String s3=String.valueOf(true);
        //转换类时必须重写ToString方法
        String s4=String.valueOf(new Student("lihua",18));
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);

        //字符串转数字
        //Integer和Double都是java中的包装类型
        int date1=Integer.parseInt("1234");
        double date2=Double.parseDouble("12.34");
        System.out.println(date1);
        System.out.println(date2);

    }
    public static void main5(String[] args) {
        String str="aaabbbcccdddcbcdcbc";
        System.out.println("charAt()方法");
        System.out.println(str.charAt(0));//a
        System.out.println("int indexOf(char ch)方法");
        System.out.println(str.indexOf('c'));//6
        System.out.println("int indexOf(char ch,int fromindex)方法");
        System.out.println(str.indexOf('c', 10));//12
        System.out.println("int indexOf(String str)方法");
        System.out.println(str.indexOf("bbb"));//3
        System.out.println("int indexOf(String str,int fromindex)方法");
        System.out.println(str.indexOf("dc",18));//-1
        System.out.println("int lastindexOf(char ch)方法");
        System.out.println(str.lastIndexOf('c'));//18
        System.out.println("int lastindexOf(char ch,int fromindex)方法");
        System.out.println(str.lastIndexOf('c',10));//8
        System.out.println("int lastindexOf(String str)方法");
        System.out.println(str.lastIndexOf("dc"));//15
        System.out.println("int lastindexOf(String str,int fromindex)方法");
        System.out.println(str.lastIndexOf("dc",14));//11
    }

    public static void main4(String[] args) {
        String s1=new String("h");
        String s2=new String("H");

        System.out.println(s1.compareToIgnoreCase(s2));
        System.out.println(s1.compareTo(s2));

        String s3=new String("hello");
        String s4=new String("hello world");
        System.out.println(s4.compareTo(s3));
    }

    public static void main3(String[] args) {
        String s1 = new String("ab");
        String s2 = new String("ab");
        System.out.println(s1.equals(s2));//true

    }
    public static void main2(String[] args) {
        //基本类型
        int a = 10;
        int b = 20;
        int c = 10;
        System.out.println(a == b);//false
        System.out.println(a == c);//true

        //引用类型
        String s1 = new String("ab");
        String s2 = new String("bc");
        String s3 = new String("ab");
        String s4 = s1;
        System.out.println(s1 == s2);//false
        System.out.println(s1 == s3);//false
        System.out.println(s1 == s4);//true
    }

    public static void main1(String[] args) {
        //使用常量串构造
        String s1="hello world";
        System.out.println(s1);

        //使用newString对象
        String s2=new String("hello world");
        System.out.println(s2);

        //使用字符数组进行构造
        char[] array={'h','e','l','l','o',' ','w','o','r','l','d'};
        String s3=new String(array);
        System.out.println(s3);
    }
}
